
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

using System.Xml;
using System.Xml.XPath;

using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Core;

namespace BackupRestoreDeploy
{
	public class BackupManagementBizRules : Syrinx.BaseBizRules
	{
		public static string DeploymentInfoFileName = "DeploymentInfo.xml";
		public IBizObjList getCompanyBackups(IApplication app, IRuleContext context)
		{
			IBizObjList backups = app.bizObjMgr.createList("CompanyBackup");
			string baseDir = Cmn.pathCombine(app.resMgr.getMacro("BaseBackupStore"), (string)ActiveCompany.get("shortName"));
			DirectoryInfo d = new DirectoryInfo(baseDir);
			d.Create();
			FileInfo[] backupFiles = d.GetFiles("*.zip");
			foreach (FileInfo f in backupFiles)
			{
				IBizObj backup = backups.insertNew();
				backup.put("id", f.Name);
				backup.put("dateCreated", f.CreationTime);
			}
			return backups;
		}

		public IBizObj getCompanyBackupDetails(IApplication app, IRuleContext context, string backupId)
		{
			IBizObj backup = null;
			string baseDir = Cmn.pathCombine(app.resMgr.getMacro("BaseBackupStore"), (string)ActiveCompany.get("shortName"));
			string fileName = Cmn.pathCombine(baseDir, backupId);
			if (File.Exists(fileName))
				using (ZipFile zipFile = new ZipFile(fileName))
				{
					ZipEntry entry = zipFile.GetEntry(BackupManagementBizRules.DeploymentInfoFileName);
					using (TextReader t = new StreamReader(zipFile.GetInputStream(entry)))
						backup = app.bizObjMgr.createSource(SourceType.BasicSource, t.ReadToEnd()).getList("Deployment", "")[0];
				}

			backup.put("origionalBackupCompanies", ((IBizObjList)backup.get("backupCompanies")).Clone());
			return backup;
		}

		public void deleteCompanyBackup(IApplication app, IRuleContext context, string backupId)
		{
			string baseDir = Cmn.pathCombine(app.resMgr.getMacro("BaseBackupStore"), (string)ActiveCompany.get("shortName"));
			string fileName = Cmn.pathCombine(baseDir, backupId);
			File.Delete(fileName);
		}

		public void restoreCompanyBackup(IApplication app, IRuleContext context, IBizObj backup)
		{
			string baseDir = Cmn.pathCombine(app.resMgr.getMacro("BaseBackupStore"), (string)ActiveCompany.get("shortName"));
			string fileName = Cmn.pathCombine(baseDir, (string)backup.get("id"));

			IBizObjList backupCompanies = (IBizObjList)backup.get("backupCompanies");
			if (backupCompanies == null || backupCompanies.numElements == 0 ||
				((IComparable)backupCompanies[0].get("id")).CompareTo(Guid.Empty) == 0)
				backup.put("backupCompanies", backup.get("restoreCompanies"));
			backup.put("filePath", fileName);
			IResults rc = context.RuleGroup.execute(context, "executeCompanyRestore", backup);
		}

		public void createCompanyBackup(IApplication app, IRuleContext context, IBizObj backup)
		{
			DateTime backupTime = DateTime.Now;
			string backupId = backupTime.ToString("yyMdhm") + ".zip";
			backup.put("id", backupId);
			backup.put("dateCreated", backupTime);
			string baseDir = Cmn.pathCombine(app.resMgr.getMacro("BaseBackupStore"), (string)ActiveCompany.get("shortName"));
			string fileName = Cmn.pathCombine(baseDir, backupId);
			IBizObjList backupCompanies = (IBizObjList)backup.get("backupCompanies");
			if (backupCompanies.numElements == 0)
				backupCompanies.Add(ActiveCompany);
			IResults rc = null;
			using (FileStream fs = File.Create(fileName))
			using (ZipOutputStream zOut = new ZipOutputStream(fs))
			{
				rc = context.RuleGroup.execute(context, "executeCompanyBackup", backup, zOut);
			}
		}

		public string getBackupRestoreDeploymentOtionDisplay(IApplication app, IRuleContext context, IBizObj bizObj, Privileges editPriv
				, string parentBPIds, string pageName)
		{
			return "BackupRestoreDeploy.Gui.AspNet.BaseDeploymentOptions";
		}
	}
}
